package com.dingwen.rabcon.config;

import com.dingwen.rabcon.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq 配置类
 *
 * @author dingwen
 * 2021.06.17 10:36
 */
//@Configuration
public class RabbitmqConfigurationFanout {

    /**
     * 声明fanout模式的交换机
     *
     * @return {@link FanoutExchange}
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        // 持久化、关闭自动删除

        // 绑定死信交换机
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-dead-letter-exchange", RabbitConstant.DIRECT_EXCHANGE_NAME_DEAD);
        args.put("x-dead-letter-routing-key", RabbitConstant.DIRECT_QUEUE__ROUTE_DEAD);
        return new FanoutExchange(RabbitConstant.FANOUT_EXCHANGE_NAME, true, false,args);
    }

    /**
     * 死信交换机
     *
     * @return {@link DirectExchange}
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitConstant.DIRECT_QUEUE_DEAD_NAME, true, false);
    }

    /**
     * 死信队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue deadQueue() {
        return new Queue(RabbitConstant.DIRECT_QUEUE_DEAD_NAME, true, false, false);
    }

    /**
     * 死信队列绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(directExchange()).with(RabbitConstant.DIRECT_QUEUE__ROUTE_DEAD);
    }

    /**
     * 短信任务队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue smsQueue() {
        // 持久化、排他性队列、关闭自动删除
        return new Queue(RabbitConstant.FANOUT_QUEUE_SMS_NAME);
    }

    /**
     * 邮件任务队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue emailQueue() {
        // 持久化、排他性队列、关闭自动删除

        // TTL队列测试
        Map<String, Object> args = new HashMap<>(1);
        // 整形 毫秒
        args.put("x-message-ttl", 5000);
        return new Queue(RabbitConstant.FANOUT_QUEUE_EMAIL_NAME, true, false, false, args);
//        return new Queue(RabbitConstant.FANOUT_QUEUE_EMAIL_NAME);
    }

    /**
     * 短信队列绑定到交换机
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding smsBinding() {
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

    /**
     * 邮箱队列绑定到交换机
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding emailBinding() {
        return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
    }

}
